home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / think / AmiChess.lha / AmiChess / src / cmd.c < prev    next >
C/C++ Source or Header  |  2002-10-31  |  10KB  |  383 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <string.h>
  5. #ifdef __GNUC__
  6. #include <unistd.h>
  7. #endif
  8. #include "common.h"
  9. #include "eval.h"
  10.  
  11. extern short stage,InChkDummy,terminal;
  12.  
  13. #ifdef HAVE_LIBREADLINE
  14. static char *inputstr;
  15. #else
  16. static char inputstr[128];
  17. #endif
  18.  
  19. static char cmd[128],file[128],s[128],logfile[128],gamefile[128],userinput[128];
  20.  
  21. char subcmd[128],setting[128],subsetting[128];
  22.  
  23. short InputCmd(char *muicmd)
  24. {
  25. short retval=1;
  26. int i;
  27. leaf *ptr; 
  28. int ncmds;
  29. char *trim;
  30.  
  31. memset(userinput,0,sizeof(userinput));
  32. memset(cmd,0,sizeof(cmd));
  33. strcpy(inputstr,muicmd);
  34. strcat(inputstr,"\n");
  35.  
  36. //      printf("%s(%d) %c ",color[board.side],(GameCnt+1)/2 + 1,prompt);
  37. if(inputstr[0]) inputstr[strlen(inputstr)-1]='\000';
  38. cmd[0]='\n';
  39. strcpy(userinput,inputstr);
  40. sscanf(inputstr,"%s %[^\n]",cmd,inputstr);
  41. if(cmd[0]=='\n') return retval;
  42. cmd[0]=subcmd[0]=setting[0]=subsetting[0]='\0';
  43. ncmds=sscanf(userinput,"%s %s %s %[^\n]",
  44. cmd,subcmd,setting,subsetting);
  45. sprintf(inputstr,"%s %s %s",subcmd,setting,subsetting);
  46. trim=inputstr+strlen(inputstr)-1;
  47. while(*trim==' ' && trim>=inputstr)
  48. *trim--='\0';
  49. if(strcmp(cmd,"help")==0) ShowHelp(inputstr);
  50. else if(strcmp(cmd,"show")==0) ShowCmd(inputstr);
  51. else if(strcmp(cmd,"debug")==0)
  52.     {
  53.     SET(flags,DEBUGG);
  54.     Debugmvl=0;
  55.     if(strcmp(inputstr,"debug")==0)
  56.         {
  57.         while(strcmp(inputstr,s))
  58.             {
  59.             sscanf(inputstr,"%s %[^\n]",s,inputstr);
  60.             ptr=ValidateMove(s);
  61.             Debugmv[Debugmvl++]=ptr->move;
  62.             MakeMove(board.side,&ptr->move);
  63.             } 
  64.         i=Debugmvl;
  65.         while(i) UnmakeMove(board.side,&Debugmv[--i]);
  66.         }
  67.     }
  68. else if(strcmp(cmd,"list")==0)
  69.     {
  70.     if(inputstr[0]=='?')
  71.         {
  72.         printf("name    - list known players alphabetically\n");
  73.         printf("score   - list by GNU best result first \n");
  74.         printf("reverse - list by GNU worst result first\n");
  75.         }
  76.     else
  77.         {
  78.         sscanf(inputstr,"%s %[^\n]",cmd,inputstr);
  79.         if(inputstr=='\000') DBListPlayer("rscore");
  80.         else DBListPlayer(inputstr);
  81.         }
  82.     }
  83. else if(strcmp(cmd,"result")==0)
  84.     {
  85.     printf("result: %s\n",inputstr);
  86.     printf("Save to %s\n",gamefile);
  87.     PGNSaveToFile(gamefile,inputstr);
  88.     DBUpdatePlayer(name,inputstr);
  89.     }    
  90. else if(strcmp(cmd,"rating")==0)
  91.     {
  92.     sscanf(inputstr,"%hd %hd",&myrating,&opprating); 
  93.     printf("my rating=%hd,opponent rating=%hd\n",myrating,opprating); 
  94.     if(opprating>=1700) bookfirstlast=2;
  95.     else if(opprating>=1700) bookfirstlast=2;
  96.     else bookfirstlast=2;
  97.     }
  98. else if(strcmp(cmd,"activate")==0)
  99.     {
  100.     CLEAR(flags,TIMEOUT);
  101.     CLEAR(flags,ENDED);
  102.     }
  103. else if(strcmp(cmd,"time")==0)
  104.     {
  105.     sscanf(inputstr,"%s %[^\n]",s,inputstr);
  106.     TimeLimit[1^board.side]=atoi(s) / 100.0f ;
  107.     }
  108. else if(strcmp(cmd,"debugply")==0) DebugPly=atoi(inputstr);
  109. else if(strcmp(cmd,"debugdepth")==0) DebugDepth=atoi(inputstr);
  110. else if(strcmp(cmd,"debugnode")==0) DebugNode=atoi(inputstr);
  111. else if(strcmp(cmd,"hash")==0)
  112.     {
  113.     sscanf(inputstr,"%s %[^\n]",cmd,inputstr);
  114.     if(strcmp(cmd,"off")==0) CLEAR(flags,USEHASH);
  115.     else if(strcmp(cmd,"on")==0) SET(flags,USEHASH);
  116.     printf("Hashing %s\n",flags & USEHASH ? "on" : "off");
  117.     }
  118. else if(strcmp(cmd,"hashsize")==0)
  119.     {
  120.     if(inputstr[0]==0)
  121.         {
  122.         printf("Current HashSize is %d slots\n",HashSize);
  123.         }
  124.     else
  125.         {
  126.         i=atoi(inputstr);
  127.         TTHashMask=0;
  128.         while((i>>=1)>0)
  129.             {
  130.             TTHashMask<<=1;
  131.             TTHashMask|=1;
  132.             }
  133.         HashSize=TTHashMask+1;
  134.         printf("Adjusting HashSize to %d slots\n",HashSize);
  135.         InitHashTable(); 
  136.         }
  137.     }
  138. else if(strcmp(cmd,"level")==0)
  139.     {
  140.     SearchDepth=0;
  141.     sscanf(inputstr,"%hd %f %hd",&TCMove,&TCTime,&TCinc);
  142.     if(TCMove==0)
  143.         {
  144.         TCMove=35 /* MIN((5*(GameCnt+1)/2)+1,60) */;
  145.         printf("TCMove=%d\n",TCMove);
  146.         suddendeath=1;
  147.         }
  148.     else suddendeath=0;
  149.     if(TCTime==0)
  150.         {
  151.         SET(flags,TIMECTL);
  152.         SearchTime=TCinc/2.0f ;
  153.         printf("Fischer increment of %d seconds\n",TCinc);
  154.         }
  155.     else
  156.         {
  157.         SET(flags,TIMECTL);
  158.         MoveLimit[white]=MoveLimit[black]=TCMove-(GameCnt+1)/2;
  159.         TimeLimit[white]=TimeLimit[black]=TCTime*60;
  160.         printf("Time Control: %d moves in %.2f secs\n",
  161.         MoveLimit[white],TimeLimit[white]);
  162.         printf("Fischer increment of %d seconds\n",TCinc);
  163.         }
  164.     }
  165. else if(strcmp(cmd,"epd")==0)
  166.     {
  167.     ParseEPD(inputstr);
  168.     NewPosition();
  169.     ShowBoard();
  170.     printf("\n%s : Best move=%s\n",id,solution); 
  171.     }
  172. else if(strcmp(cmd,"setboard")==0)
  173.     {
  174.     ParseEPD(inputstr);
  175.     NewPosition();
  176.     }
  177. else if(strcmp(cmd,"ping")==0)
  178.     {
  179.     printf("pong %s\n",inputstr);
  180.     fflush(stdout);
  181.     }
  182. else if(strcmp(cmd,"solve")==0||strcmp(cmd,"solveepd")==0) Solve(inputstr);
  183. else if(strcmp(cmd,"st")==0)
  184.     {
  185.     sscanf(inputstr,"%hd",&TCinc);
  186.     suddendeath=0 ;
  187.     SearchTime=TCinc*0.90f;
  188.     CLEAR(flags,TIMECTL);
  189.     }
  190. else retval=0;
  191. return retval;
  192. }
  193.  
  194. void ShowCmd(char *subcmd)
  195. {
  196. char cmd[10];
  197. sscanf(subcmd,"%s %[^\n]",cmd,subcmd);
  198. if(strcmp(cmd,"rating")==0)
  199.     {
  200.     printf("My rating=%d\n",myrating);
  201.     printf("Opponent rating=%d\n",opprating);
  202.     }
  203. else if(strcmp(cmd,"time")==0) ShowTime();
  204. else if(strcmp(cmd,"moves")==0)
  205.     {
  206.     GenCnt=0;
  207.     TreePtr[2]=TreePtr[1];
  208.     GenMoves(1);      
  209.     ShowMoveList(1);
  210.     printf("No. of moves generated=%ld\n",GenCnt);
  211.     }
  212. else if(strcmp(cmd,"escape")==0)
  213.     {
  214.     GenCnt=0;
  215.     TreePtr[2]=TreePtr[1];
  216.     GenCheckEscapes(1);      
  217.     ShowMoveList(1);
  218.     printf("No. of moves generated=%ld\n",GenCnt);
  219.     }
  220. else if(strcmp(cmd,"noncapture")==0)
  221.     {
  222.     GenCnt=0;
  223.     TreePtr[2]=TreePtr[1];
  224.     GenNonCaptures(1);      
  225.     FilterIllegalMoves(1);
  226.     ShowMoveList(1);
  227.     printf("No. of moves generated=%ld\n",GenCnt);
  228.     }
  229. else if(strcmp(cmd,"capture")==0)
  230.     {
  231.     GenCnt=0;
  232.     TreePtr[2]=TreePtr[1];
  233.     GenCaptures(1);      
  234.     FilterIllegalMoves(1);
  235.     ShowMoveList(1);
  236.     printf("No. of moves generated=%ld\n",GenCnt);
  237.     }
  238. else if(strcmp(cmd,"eval")==0||strcmp(cmd,"score")==0)
  239.     {
  240.     int s,wp,bp,wk,bk;
  241.     short r,c,sq;
  242.     BitBoard *b;
  243.     phase=PHASE;
  244.     GenAtaks();
  245.     FindPins(&pinned);
  246.     hunged[white]=EvalHung(white);
  247.     hunged[black]=EvalHung(black);
  248.     b=board.b[white];
  249.     pieces[white]=b[knight]|b[bishop]|b[rook]|b[queen]; 
  250.     b=board.b[black];
  251.     pieces[black]=b[knight]|b[bishop]|b[rook]|b[queen]; 
  252.     wp=ScoreP(white);
  253.     bp=ScoreP(black);
  254.     wk=ScoreK(white);
  255.     bk=ScoreK(black);
  256.     printf("White:  Mat:%4d/%4d  P:%d  N:%d  B:%d  R:%d  Q:%d  K:%d  Dev:%d  h:%d x:%d\n",board.pmaterial[white],board.material[white],wp,ScoreN(white),ScoreB(white),ScoreR(white),ScoreQ(white),wk,ScoreDev(white),hunged[white],ExchCnt[white]);
  257.     printf("Black:  Mat:%4d/%4d  P:%d  N:%d  B:%d  R:%d  Q:%d  K:%d  Dev:%d  h:%d x:%d\n",board.pmaterial[black],board.material[black],bp,ScoreN(black),ScoreB(black),ScoreR(black),ScoreQ(black),bk,ScoreDev(black),hunged[black],ExchCnt[black]);
  258.     printf("Phase: %d\t",PHASE);
  259.     s=(EvaluateDraw()?DRAWSCORE:Evaluate(-INFINITY,INFINITY));
  260.     printf("score=%d\n\n",s);
  261. //    return;
  262.     for(r=56;r>=0;r-= 8)
  263.         {
  264.         printf("     +---+---+---+---+---+---+---+---+\n");
  265.         printf("   %d |",(r>>3)+1);
  266.         for(c=0;c<8;c++)
  267.             {
  268.             sq=r+c;
  269.             if(cboard[sq]==0) printf("   |");
  270.             else printf("%3d|",pscore[sq]);
  271.             }
  272.         printf("\n");
  273.         }
  274.     printf("     +---+---+---+---+---+---+---+---+\n");
  275.     printf("       a   b   c   d   e   f   g   h  \n");
  276.     }
  277. else if(strcmp(cmd,"game")==0) ShowGame();
  278. else if(strcmp(cmd,"pin")==0)
  279.     {
  280.     BitBoard b;
  281.     GenAtaks();
  282.     FindPins(&b);
  283.     ShowBitBoard(&b);
  284.     }
  285. }
  286.  
  287. void BookCmd(char *subcmd)
  288. {
  289. char cmd[10];
  290. char bookfile[64];
  291. short bookply;
  292.  
  293. sscanf(subcmd,"%s %[^\n]",cmd,subcmd);
  294. if(strcmp(cmd,"make")==0) sscanf(subcmd,"%s %hd\n",bookfile,&bookply);
  295. }
  296.  
  297. static const char *const helpstr[]={
  298. "^C",
  299. " Typically the interrupt key stops a search in progress,",
  300. " makes the move last considered best and returns to the",
  301. " command prompt",
  302. "help",
  303. " Produces a help blurb corresponding to this list of commands.",
  304. "pgnsave FILENAME",
  305. " saves the game so far to the file from memory",
  306. "pgnload FILENAME",
  307. " loads the game in the file into memory",
  308. "name NAME",
  309. " Lets you input your name. Also writes the log.nnn and a",
  310. " corresponding game.nnn file. For details please see",
  311. " auxillary file format sections.",
  312. "activate",
  313. " This command reactivates a game that has been terminated automatically",
  314. " due to checkmate or no more time on the clock. However,it does not",
  315. " alter those conditions. You would have to undo a move or two or",
  316. " add time to the clock with level or time in that case.",
  317. "rating COMPUTERRATING OPPONENTRATING",
  318. " Inputs the estimated rating for computer and for its opponent",
  319. "time",
  320. " Inputs time left in game for computer in hundredths of a second.",
  321. " Mostly used by Internet Chess server.",
  322. "hashsize N",
  323. " Sets the hash table to permit storage of N positions",
  324. "level MOVES MINUTES INCREMENT",
  325. " Sets time control to be MOVES in MINUTES with each move giving",
  326. " an INCREMENT(in seconds,i.e. Fischer-style clock).",
  327. "solve FILENAME",
  328. "solveepd FILENAME",
  329. " Solves the positions in FILENAME",
  330. "show",
  331. " board - displays the current board",
  332. " time - displays the time settings",
  333. " moves - shows all moves using one call to routine",
  334. " escape - shows moves that escape from check using one call to routine",
  335. " noncapture - shows non-capture moves",
  336. " capture - shows capture moves",
  337. " eval [or score] - shows the evaluation per piece and overall",
  338. " game - shows moves in game history",
  339. " pin - shows pinned pieces",
  340. " evalspeed tests speed of the evaluator",
  341. NULL,
  342. NULL
  343. };
  344.  
  345. void ShowHelp(const char *command)
  346. {
  347. const char * const *p;
  348. int count,len;
  349. if(strlen(command)>0)
  350.     {
  351.     for(p=helpstr,count=0;*p;p++)
  352.         {
  353.         if(strncmp(*p,inputstr,strlen(command))==0)
  354.             {
  355.             puts(*p);
  356.             while(*++p&&**p!= ' ')
  357.             for(;*p &&**p==' ';p++)
  358.                 {
  359.                 puts(*p);
  360.                 }
  361.             return;
  362.             }
  363.         }
  364.     printf("Help for command %s not found\n\n",command);
  365.     }
  366. printf("List of commands:(help COMMAND to get more help)\n");
  367. for(p=helpstr,count=0;*p;p++)
  368.     {
  369.     len=strcspn(*p," ");
  370.     if(len>0)
  371.         {
  372.         count+=printf("%.*s  ",len,*p);
  373.         if(count>60)
  374.             {
  375.             count=0;
  376.             puts("");
  377.             }
  378.         }
  379.     }
  380. puts("");
  381. }
  382.  
  383.